package cn.wjc.demo.part.three.service.impl;

import cn.wjc.demo.common.entity.Student;
import cn.wjc.demo.common.entity.Teacher;
import cn.wjc.demo.part.three.client.RemoteAddStudentFeignService;
import cn.wjc.demo.part.three.client.RemoteAddTeacherFeignService;
import cn.wjc.demo.part.three.service.IAddService;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AddServiceImpl implements IAddService {

    private final RemoteAddStudentFeignService addStudentFeignService;
    private final RemoteAddTeacherFeignService addTeacherFeignService;

    @GlobalTransactional
    @Override
    public void remoteAddStudentAndTeacher() {
        System.out.println("开始执行【Partthree 添加学生和老师】的业务……");
        Student student = new Student();
        Double sId = (Math.random()*10000+1000);
        student.setSId(sId.toString());
        student.setSName("网易云");
        Teacher teacher = new Teacher();
        teacher.setTId(sId.toString());
        teacher.setTName("昊殷");

        System.out.println("seata全局事务id====================>"+ RootContext.getXID());

        System.out.println("partthree远程调用partone模块添加老师...");
        addTeacherFeignService.remoteAddTeacher(teacher);
        double x = 0;
//        if ( (x=Math.random()) > 0.5 ) {
//            System.out.println("Partthree 远程调用出现异常!");
//            throw new RuntimeException();
//        }
        System.out.println("partthree远程调用parttwo模块添加学生...");
        addStudentFeignService.remoteAddStudent(student);
        System.out.println("执行【Partthree 添加学生和老师】的业务完成！");
    }
}
